package com.jy.event.pay.result;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by lihu on 2017/10/9.
 * 威福通返回实体
 */
@Data
@XStreamAlias("xml")
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class PayResult {

    @XStreamAlias("version")
    private String version;

    @XStreamAlias("charset")
    private String charset;

    @XStreamAlias("sign_type")
    private String signType;

    @XStreamAlias("status")
    private String status;

    @XStreamAlias("message")
    private String message;

    @XStreamAlias("result_code")
    private String resultCode;

    @XStreamAlias("mch_id")
    private String mchId;

    @XStreamAlias("device_info")
    private String deviceInfo;

    @XStreamAlias("nonce_str")
    private String nonceStr;

    @XStreamAlias("err_code")
    private String errCode;

    @XStreamAlias("err_msg")
    private String errMsg;

    @XStreamAlias("code_url")
    private String codeUrl;

    @XStreamAlias("code_img_url")
    private String codeImgUrl;

    @XStreamAlias("need_query")
    private String need_query;


    @XStreamAlias("openid")
    private String openid;

    @XStreamAlias("sub_openid")
    private String subOpenid;

    //SUCCESS—支付成功REFUND—转入退款NOTPAY—未支付CLOSED—已关闭REVERSE—已冲正REVOK—已撤销
    @XStreamAlias("trade_state")
    private String tradeState;

    /**
     * pay.weixin.micropay——微信刷卡支付
     * pay.alipay.micropay——支付宝刷卡支付
     * pay.jdpay.micropay——京东刷卡支付
     * pay.qq.micropay——QQ钱包刷卡支付
     * pay.shiming.micropay——会员卡支付
     * pay.unionpay.micropay——银联支付
     * pay.bestpay.micropay——翼支付
     */
    @XStreamAlias("trade_type")
    private String tradeType;

    @XStreamAlias("is_subscribe")
    private String isSubscribe;

    @XStreamAlias("pay_result")
    private String payResult;

    @XStreamAlias("transaction_id")
    private String transactionId;

    @XStreamAlias("out_transaction_id")
    private String outTransactionId;

    @XStreamAlias("sub_is_subscribe")
    private String subIsSubscribe;

    @XStreamAlias("sub_appid")
    private String subAppid;

    @XStreamAlias("out_trade_no")
    private String outTradeNo;

    @XStreamAlias("total_fee")
    private int totalFee;

    @XStreamAlias("coupon_fee")
    private int couponFee;

    @XStreamAlias("fee_type")
    private String feeType;

    @XStreamAlias("attach")
    private String attach;

    @XStreamAlias("bank_type")
    private String bankType;

    @XStreamAlias("bank_billno")
    private String bankBillno;

    @XStreamAlias("time_end")
    private String timeEnd;

    @XStreamAlias("token_id")
    private String tokenId;

    @XStreamAlias("pay_info")
    private String payInfo;

    /**
     * 校验支付结果
     *
     * @return
     */
    public boolean success() {
        if (StringUtils.equals("0", status) &&
                StringUtils.equals("0", resultCode) &&
                StringUtils.equals("SUCCESS", tradeState)) {
            return true;
        }
        return false;
    }

    /**
     * 校验是否继续轮询
     *
     * @return
     */
    public boolean accepted() {
        if (StringUtils.equals("NOTPAY", tradeState) ||
                StringUtils.equals("USERPAYING", tradeState)) {
            return true;
        }
        return false;
    }


    /**
     * 校验退款结果
     *
     * @return
     */
    public boolean successrefund() {
        if (((StringUtils.equals("0", status) &&
                StringUtils.equals("0", resultCode))) ||
                StringUtils.equals(errCode, "ACQ.TRADE_NOT_EXIST")) {
            return true;
        }
        return false;
    }

}
